﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Text;
using System.IO;
using System.Security.Cryptography;

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string tt = MD5Manager.Md5Encrypt(TextBox2.Text.Trim());
        SqlConnection myConn = GetConnection();
        myConn.Open();
        // SqlCommand cmd=new SqlCommand();
        string sqlStr = "select * from [Users] where [UID]='" + TextBox1.Text.Trim() + "' and [UPassword]='" + tt + "'";
        SqlCommand cmd = new SqlCommand(sqlStr, myConn);
        cmd.ExecuteNonQuery();
        SqlDataReader read = cmd.ExecuteReader();
        if (read.Read())
        {
            Session.Timeout = 1440;
            Session["id"] = this.TextBox1.Text;
            Session["name"] = read["UName"].ToString();
            Session["power"] = read["UPower"].ToString();
            TextBox1.Text = "";
            TextBox2.Text = "";
            read.Close();
            Response.Redirect("~/index.aspx?");
        }
        else
        {
            read.Close();
            Response.Write("<script>alert('用户名或密码错误!')</script>");
        }
    }
    public SqlConnection GetConnection()
    {
        string myStr = ConfigurationManager.AppSettings["ConnectionString"].ToString();
        SqlConnection myConn = new SqlConnection(myStr);
        return myConn;
    }
}
class MD5Manager
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="strSource"></param>
    /// <returns></returns>
    public static string Md5Encrypt(string strSource)
    {
        byte[] bytIn = System.Text.Encoding.Default.GetBytes(strSource);
        byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };
        byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };

        DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
        mobjCryptoService.IV = iv;
        mobjCryptoService.Key = key;

        ICryptoTransform encrypto = mobjCryptoService.CreateEncryptor();
        MemoryStream ms = new MemoryStream();
        CryptoStream cs = new CryptoStream(ms, encrypto, CryptoStreamMode.Write);
        cs.Write(bytIn, 0, bytIn.Length);
        cs.FlushFinalBlock();

        return System.Convert.ToBase64String(ms.ToArray());

    }
    /// <summary>
    /// 
    /// </summary>
    /// <param name="strSource"></param>
    /// <returns></returns>
    public static string Md5Decrypt(string strSource)
    {
        byte[] bytIn = System.Convert.FromBase64String(strSource);
        byte[] iv = { 102, 16, 93, 156, 78, 4, 218, 32 };
        byte[] key = { 55, 103, 246, 79, 36, 99, 167, 3 };

        DESCryptoServiceProvider mobjCryptoService = new DESCryptoServiceProvider();
        mobjCryptoService.IV = iv;
        mobjCryptoService.Key = key;

        ICryptoTransform decrypto = mobjCryptoService.CreateDecryptor();
        MemoryStream ms = new MemoryStream(bytIn, 0, bytIn.Length);
        CryptoStream cs = new CryptoStream(ms, decrypto, CryptoStreamMode.Read);
        StreamReader sr = new StreamReader(cs, Encoding.Default);

        return sr.ReadToEnd();
    }
}